package com.uplift.common.utils;

import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PoiUtil2 {
    public static void copySheet(XSSFSheet srcSheet, XSSFSheet destSheet,
                         XSSFWorkbook srcwb, XSSFWorkbook destwb) throws Exception {
        int rowCount = srcSheet.getLastRowNum();// 总行数
        int maxCellNum = 0;
        // System.out.println("------total row=------" + rowCount + "---="+
        // srcSheet.getPhysicalNumberOfRows());
        XSSFRow srcRow = null, destRow = null;
        //注意这里
        for (int i = 0; i <=rowCount; i++) {
            srcRow = srcSheet.getRow(i);
            destRow = destSheet.getRow(i);
            if (srcRow == null) {
                continue;
            }
            // 最大列数
            maxCellNum = maxCellNum < srcRow.getLastCellNum() ? srcRow
                    .getLastCellNum() : maxCellNum;
            if (destRow == null) {
                destRow = destSheet.createRow(i);
            }
            // 设置行高
            destRow.setHeight(srcRow.getHeight());
            // System.out.println("---------row=" + i + "---="+
            // srcRow.getPhysicalNumberOfCells() +
            // "----="+srcRow.getLastCellNum());
            copySheetRow(srcRow, destRow, srcwb, destwb);
            srcRow = null;
            destRow = null;
        }
        srcRow = null;
        destRow = null;
//        return maxCellNum;
    }

    private static void copySheetRow(XSSFRow srcRow, XSSFRow destRow,
                              XSSFWorkbook srcwb, XSSFWorkbook destwb) {
        int cellCount = srcRow.getLastCellNum();// 每行的总列数
        XSSFCell srcCell = null, destCell = null;
        XSSFCellStyle srcCellStyle = null, destCellStyle = null;
        for (int j = 0; j < cellCount; j++) {// 遍历行单元格
            srcCell = srcRow.getCell(j);
            destCell = destRow.getCell(j);
            if (destCell == null) {
                destCell = destRow.createCell(j);
            }
            if (srcCell != null) {
                srcCellStyle = srcCell.getCellStyle();// 原sheet页样式
                destCellStyle = null;
                destCellStyle = destCell.getCellStyle();
                // 复制样式
                destCellStyle.cloneStyleFrom(srcCellStyle);
                // 处理单元格内容
                switch (srcCell.getCellType()) {
                    case STRING:
                        destCell.setCellValue(srcCell.getRichStringCellValue());
                        break;
                    // 这里判断是否是日期
                    case NUMERIC:
                        // 判断是否是日期格式
                        // 测试发现如果这里不新建样式,日期显示的是数字
                        if (DateUtil.isCellDateFormatted(srcCell)) {
                            // 新建样式
                            destCellStyle = destwb.createCellStyle();
                            // 复制样式
                            destCellStyle.cloneStyleFrom(srcCellStyle);
                            destCell.setCellStyle(destCellStyle);
                            destCell.setCellValue(srcCell.getDateCellValue());
                        } else {
                            destCell.setCellValue(srcCell.getNumericCellValue());
                        }
                        break;
                    case FORMULA:
                        destCell.setCellFormula(srcCell.getCellFormula());
                        break;
                    case BOOLEAN:
                        destCell.setCellValue(srcCell.getBooleanCellValue());
                        break;
                    case BLANK:
//                        destCell.setCellType(s);
                        break;
                    case ERROR:
                        break;
                    default:
                        break;
                }
            }
        }
        srcCellStyle = null;
        destCellStyle = null;
        srcCell = null;
        destCell = null;
    }

    public void mergeSheetAllRegion(XSSFSheet srcSheet, XSSFSheet destSheet) {
        int num = srcSheet.getNumMergedRegions();
        CellRangeAddress cellR = null;
        for (int i = 0; i < num; i++) {
            cellR = srcSheet.getMergedRegion(i);
            destSheet.addMergedRegion(cellR);
        }
    }

    public void setSheetWidth(XSSFSheet srcSheet, XSSFSheet destSheet,
                              int maxCellNum) {
        for (int i = 0; i <= maxCellNum; i++) {
            destSheet.setColumnWidth(i, srcSheet.getColumnWidth(i));
        }
    }



}
